function [ Portfolio, PutPrice] = deltaHedge2( Price, Strike, Time, r, sigma, miu, steps, nSim )
% This function use monte carlo to simulate 
% delta hedging and calculate the hedging error
% due to discrete hedging.
% Assume option is a put option

% Initialization
S0 = Price; K = Strike; T = Time;
N = steps; M = nSim;
dt = T/N;

Xmax = (miu-0.5*sigma^2)*T + 3*sigma*sqrt(T);
Xmin = (miu-0.5*sigma^2)*T - 3*sigma*sqrt(T);
Smax = S0*exp(Xmax);
Smin = S0*exp(Xmin);

% Possible Asset Prices
S = Smin:(Smax-Smin)/99:Smax;
% Time to expiry
t = T:-dt:0;
% Replace last time to expiration small value
t(N+1) = dt/1000;

[tMat,SMat] = meshgrid(t,S); % Generate time and price matrix for delta
[CallDelta, PutDelta] = blsdelta(SMat, K, r, tMat, sigma);
deltaMat = PutDelta;

% Now we simulate Monte Carlo asset prices
% Initialize Asset price, Bank account, Alpha Vector, Option Vector
[OptionCall0, OptionPut0] = blsprice(S0,K,r,T,sigma);
[DeltaCall0, DeltaPut0] = blsdelta(S0,K,r,T,sigma);
Alpha0 = DeltaPut0*ones(M,1);
Option0 = OptionPut0*ones(M,1);
Bank0 = - Alpha0*S0;
AssetPrice = S0*ones(M,1);
% Time Stepping by vector
for i=1:N
    % Generate random vector of asset price
    dWt = randn(M,1);  % Wiener process
    multiple = exp((miu-0.5*sigma^2)*dt + sigma*sqrt(dt).*dWt);
    AssetPrice = AssetPrice.*multiple;
    % We now correct price vector for Smax and Smin
    tempMax = AssetPrice>Smax;
    tempMin = AssetPrice<Smin;
    tempOrg = (1-tempMax).*(1-tempMin);
    AssetPrice = tempMax.*Smax + tempMin.*Smin + tempOrg.*AssetPrice;
    
    % We now balance our portfolio based on the updated price
    Alpha1 = interp1(SMat(:,i+1),deltaMat(:,i+1),AssetPrice);
    Bank1 = exp(r*dt)*Bank0 - AssetPrice.*(Alpha1 - Alpha0);
    %Portfolio = -Option0 + Alpha0.*AssetPrice + Bank1;
    
    % Update Hedging Parameter Vectors
    Alpha0 = Alpha1;
    Bank0 = Bank1;
end

% Now we get the final liquidation value for the portfolio at T
%Portfolio = -max(AssetPrice-K,0) + Alpha1.*AssetPrice + Bank1;
Portfolio = AssetPrice + Alpha1.*AssetPrice + Bank1;
% Discount the Portfolio to time 0
Portfolio = exp(-r*T)*Portfolio;
PutPrice = OptionPut0;

end

